//用于给用户选择要进入哪个项目的grid
var indexBeforeChooseProjectGrid = Ext.create('Ext.grid.Panel', {
    overflowX: 'hidden', //设置横向滚动条不显示
    overflowY: 'auto', //设置纵向滚动条超出高度显示
    border: false,
    store: indexBeforeChooseProjectGridStore,
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '项目名称',
            flex: 1,
            dataIndex: 'name',
            sortable: false,
            filter: {//用于形成搜索框
                type: 'string',
                emptyText: '输入要搜索的项目名字',
                serializer: function (filter) {
                    return filter.value;
                }
            }
        }, {
            text: '测试系统',
            flex: 1,
            dataIndex: 'systemName',
            sortable: false
        }, {
            text: '负责人',
            flex: 1,
            dataIndex: 'leadingCadreName',
            sortable: false
        }, {
            text: '操作',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 90,
            items: [{
                icon: 'static/images/icons/fam/into.ico',
                tooltip: '进入该项目',
                handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                    var projectId = record.data.id;
                    Ext.Ajax.request({//去更新缓存里的项目选择
                        url: zuulUrl + '/' + webServiceName + '/cache/inProject',
                        method: 'put',
                        params: {
                            userId: currentUserId,
                            projectId: projectId
                        },
                        async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                        success: function (response, opts) {
                            document.location.href = '/training-web-service/training';//刷新成功就跳转到主页
                        },
                        failure: function (response, opts) {
                            var obj = Ext.decode(response.responseText);
                            if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                                Ext.Msg.alert("错误", obj.message);
                            else
                                Ext.Msg.alert("错误", "该功能暂时不可用！");
                        }
                    });
                }
            }]
        }
    ],
    plugins: {
        rowexpander: {
            rowBodyTpl: new Ext.XTemplate(
                '<b>项目详情:</b> {describe}'
            )
        },
        gridfilters: true,//启用filters
    },
    features: [{
        id: 'group',
        ftype: 'groupingsummary',
        groupHeaderTpl: new Ext.XTemplate(
            '所属小组: {[values.name.substring(32)]}'
        ),
        hideGroupedHeader: false,
        enableGroupingMenu: false
    }]
});

//用于给用户选择要加入哪个班级的grid
var indexBeforeChooseEnterClassesGrid = Ext.create('Ext.grid.Panel', {
    overflowX: 'hidden', //设置横向滚动条不显示
    overflowY: 'auto', //设置纵向滚动条超出高度显示
    store: indexBeforeChooseEnterClassesGridStore,
    border: false,
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '班级名称',
            flex: 1,
            dataIndex: 'name',
            sortable: false,
            filter: {//用于形成搜索框
                type: 'string',
                emptyText: '输入要搜索的项目名字',
                serializer: function (filter) {
                    return filter.value;
                }
            }
        }, {
            text: '期数',
            flex: 1,
            dataIndex: 'stage',
            sortable: false,
            renderer: function (v, cell) {
                return '第' + v + '期';
            },
        }, {
            text: '开始时间',
            flex: 1,
            dataIndex: 'beginTime',
            sortable: false,
            renderer: function (v, record) {
                return v.replace("T", " ");
            },
        }, {
            text: '结束时间',
            flex: 1,
            dataIndex: 'endTime',
            sortable: false,
            renderer: function (v, cell) {
                return v.replace("T", " ");
            },
        }, {
            text: '人数状态',
            flex: 1,
            sortable: false,
            renderer: function (v, cell) {
                return cell.record.data.nowCount + '/' + cell.record.data.maxMemberCount;
            },
        }, {
            text: '状态',
            dataIndex: 'isHadIn',
            align: 'center',
            sortable: false,
            renderer: function (v, cell) {
                return v ? '已报名' : '未报名';
            },
        }, {
            text: '操作',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 90,
            items: [{
                icon: 'static/images/icons/fam/add.png',
                tooltip: '报名该班级',
                handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                    var classesId = record.data.id;
                    if (record.get('isHadIn'))
                        Ext.Msg.alert("错误", "你已报名该班级！");
                    else {
                        Ext.Ajax.request({
                            url: zuulUrl + '/' + authServiceName + '/user/classes',
                            method: 'post',
                            params: {
                                currentUserId: currentUserId,
                                classesId: classesId
                            },
                            async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                            success: function (response, opts) {
                                var nowCount = response.responseText;
                                if (nowCount == -1)
                                    Ext.Msg.alert("错误", "该班级报名已结束！");
                                else {
                                    Ext.Msg.alert("成功", "报名成功");
                                    if (nowCount >= record.get('maxMemberCount'))//如果报名之后满足了开班条件就把它移除
                                        indexBeforeChooseEnterClassesGridStore.remove(record);
                                    else {
                                        record.set('nowCount', nowCount);
                                        record.set('isHadIn', true);
                                    }
                                }
                            },
                            failure: function (response, opts) {
                                var obj = Ext.decode(response.responseText);
                                if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                                    Ext.Msg.alert("错误", obj.message);
                                else
                                    Ext.Msg.alert("错误", "该功能暂时不可用！");
                            }
                        });
                    }
                }
            }, {
                icon: 'static/images/icons/fam/cross.gif',
                tooltip: '取消报名',
                handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                    var classesId = record.data.id;
                    if (!record.get('isHadIn'))
                        Ext.Msg.alert("错误", "你没有报名该班级！");
                    else {
                        Ext.Ajax.request({
                            url: zuulUrl + '/' + authServiceName + '/user/classes',
                            method: 'put',
                            params: {
                                currentUserId: currentUserId,
                                classesId: classesId
                            },
                            async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                            success: function (response, opts) {
                                var nowCount = response.responseText;
                                if (nowCount == -1)
                                    Ext.Msg.alert("错误", "该班级报名操作已结束！");
                                else {
                                    Ext.Msg.alert("成功", "取消报名成功");
                                    record.set('nowCount', nowCount);
                                    record.set('isHadIn', false);
                                }
                            },
                            failure: function (response, opts) {
                                var obj = Ext.decode(response.responseText);
                                if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                                    Ext.Msg.alert("错误", obj.message);
                                else
                                    Ext.Msg.alert("错误", "该功能暂时不可用！");
                            }
                        });
                    }
                }
            }]
        }
    ],
    plugins: {
        rowexpander: {
            rowBodyTpl: new Ext.XTemplate(
                '<b>班级详情:</b> {describe}'
            )
        },
        gridfilters: true,//启用filters
    }
});